package com.vince.hulk.character
{
import com.vince.hulk.states.PlayState;

import org.flixel.FlxG;
import org.flixel.FlxObject;
import org.flixel.FlxSprite;

/**
 * author yangwz
 */		
public class Character extends FlxSprite
{
	public function Character(X:Number=0, Y:Number=0, SimpleGraphic:Class=null)
	{
		super(X, Y, SimpleGraphic);
		initDisplay();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	protected var _moveSpeed:int;
	protected var _jumpPower:int;
	protected var isWalkingLeft:Boolean;
	protected var isWalkingRight:Boolean;
	protected var externalForce:Number;
	private var SoundLand:Class;
	//==========================================================================
	//  Properties
	//==========================================================================
	public function get jumpPower():int
	{
		return _jumpPower;
	}
	
	public function get moveSpeed():int
	{
		return _moveSpeed;
	}
	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		_moveSpeed = 0;
		_jumpPower = 0;
		maxVelocity.x = 350;
		maxVelocity.y = 500;
		acceleration.y = 1000;
		drag.x = 400;
		(FlxG.state as PlayState).characterGroup.add(this);
		isWalkingLeft = false;
		isWalkingRight = false;
		externalForce = 0;
	}
	
	public function split():Array
	{
		return [];
	}
	
	public function size():int
	{
		return 1;
	}
	
	public function setCouldntMerge():void
	{
		return;
	}
	
	public function startWalking(value:Boolean):void
	{
		if (value)
		{
			isWalkingRight = false;
			isWalkingLeft = true;
		}
		else
		{
			isWalkingLeft = false;
			isWalkingRight = true;
		}
	}
	
	public function merge(character:Character):Character
	{
		return this;
	}
	
	/**
	 * 添加外力 
	 * @param value
	 * 
	 */	
	public function applyForce(value:int):void
	{
		externalForce = externalForce + value;
	}
	
	/**
	 * 停止走动 
	 */	
	public function stopWalking():void
	{
		isWalkingLeft = false;
		isWalkingRight = false;
	}
	
	protected function isOverlapping(character:Character):Boolean
	{
		var flxSprite:FlxSprite = null;
		var state:* = PlayState(FlxG.state);
		for each (flxSprite in state.doors.members)
		{
			if (flxSprite.alive && character.overlaps(flxSprite))
			{
				return true;
			}
		}
		if (character.overlaps(state._map1) || state.collidableMechanisms.members.length != 0 && character.overlaps(state.collidableMechanisms) || state.blocks.members.length != 0 && character.overlaps(state.blocks) || state.movingBlocks.members.length != 0 && character.overlaps(state.movingBlocks))
		{
			return true;
		}
		return false;
	}
	
	override public function update():void
	{
		var targetX:Number = 0;
		if (justTouched(FLOOR))
		{
//			FlxG.play(this.SoundLand, 0.4);
		}
		if (isWalkingLeft)
		{
			targetX = -this._moveSpeed;
		}
		else if (isWalkingRight)
		{
			targetX = this._moveSpeed;
		}
		if (isTouching(FlxObject.LEFT) || isTouching(FlxObject.RIGHT))
		{
			externalForce = 0;
		}
		else if (isTouching(FlxObject.DOWN))
		{
			if (externalForce > 0)
			{
				externalForce = Math.max(externalForce - drag.x / 10, 0);
			}
			else
			{
				externalForce = Math.min(externalForce + drag.x / 10, 0);
			}
		}
		else if (externalForce > 0)
		{
			externalForce = Math.max(externalForce - drag.x / 100, 0);
		}
		else
		{
			externalForce = Math.min(externalForce + drag.x / 100, 0);
		}
		targetX = targetX + externalForce;
		velocity.x = targetX;
	}
}
}